import {
  Controller,
  Post,
  Body,
  UseGuards,
  HttpCode,
  Get,
  Query,
  Param,
  Req,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiTags, ApiHeader } from '@nestjs/swagger';
import { PostService } from './post.service';
import { PostDto } from './dto/post.dto';

@ApiTags('发帖模块')
@Controller('admin')
@UseGuards(AuthGuard('jwt'))
export class PostController {
  constructor(private readonly postService: PostService) {}

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Post('/createPost')
  // 创建帖子
  createPost(@Req() request, @Body() postDto: PostDto) {
    if (postDto.coverSize == 3 && postDto.coverUrlList.length !== 3) {
      return { code: 1, msg: '图片不能少于3张', data: '' };
    }
    postDto.userId = request.user.userId;
    return this.postService.createPost(postDto);
  }

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Post('/updatePost/:postId')
  // 更新帖子
  updatePost(
    @Param('postId') postId: string,
    @Body() postDto: PostDto,
    @Req() request,
  ) {
    if (postId.length !== 18) {
      return {
        code: 1,
        msg: '参数错误',
      };
    }
    if (postDto.coverSize == 3 && postDto.coverUrlList.length !== 3) {
      return { code: 1, msg: '图片不能少于3张', data: '' };
    }
    postDto.userId = request.user.userId;
    return this.postService.updatePostByPostId(postId, postDto);
  }

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Post('/createSchedulePost')
  // 定时创建帖子
  async createSchedulePost(@Req() request, @Body() postDto: PostDto) {
    if (postDto.coverSize == 3 && postDto.coverUrlList.length !== 3) {
      return { code: 1, msg: '图片不能少于3张' };
    }
    if (!postDto.scheduleTime) {
      return { code: 1, msg: '定时任务时间不能为空' };
    }
    if (Number(postDto.scheduleTime) < new Date().getTime()) {
      return { code: 1, msg: '定时任务时间不能小于当前时间' };
    }
    postDto.userId = request.user.userId;
    const bool = await this.postService.createSchedulePost(postDto);
    if (bool) {
      return { msg: '定时任务创建成功' };
    }
  }

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Get('/queryPostList')
  // 获取帖子列表
  queryPostList(@Query() query, @Req() request) {
    query.pageIndex = query.pageIndex ? query.pageIndex : 0;
    query.pageSize = query.pageSize ? query.pageSize : 10;
    query.keyWord = query.keyWord ? query.keyWord : '';
    query.beginTime = query.beginTime ? query.beginTime : '';
    query.endTime = query.endTime ? query.endTime : '';
    return this.postService.queryPostList(
      query.pageIndex,
      query.pageSize,
      query.keyWord,
      query.beginTime,
      query.endTime,
      request.user.userId,
    );
  }

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Get('/queryPostDetail')
  // 获取帖子详情
  queryPostDetail(@Query('postId') postId: string) {
    if (!postId) {
      return {
        msg: '查询参数为空',
      };
    }
    return this.postService.queryPostDetail(postId);
  }

  @ApiHeader({
    name: 'access-token',
    required: true,
    description: 'token',
  })
  @HttpCode(200)
  @Get('/deletePost/:postId')
  // 删除帖子
  deletePost(@Param('postId') postId: string) {
    if (postId.length !== 18) {
      return {
        msg: '参数错误',
      };
    }
    return this.postService.deletePost(postId);
  }
}
